package com.xisoft.bx.service.billService;

import com.alibaba.fastjson.JSONObject;
import com.xisoft.action.SuperAction;
import com.xisoft.bx.config.bill.*;
import com.xisoft.entity.reference.Reference;
import com.xisoft.entity.config.param.Config;
import com.xisoft.entity.config.param.ConfigQuery;
import com.xisoft.handle.HandlerConfigChain;
import com.xisoft.queue.SourceQueue;
import com.xisoft.utils.PubConvert;
import com.xisoft.utils.PubResult;
import com.xisoft.vo.ConfigVO;
import com.xisoft.vo.QueryVO;
import com.xisoft.security.CurrentUser;
import lombok.extern.log4j.Log4j2;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * @Author xzh
 * @Date 2022/9/6 11:20
 * @Description
 * @Version 1.0
 **/
@Log4j2
@Service("BillAppService")
public class BillAppService  extends BillServiceImpl{

    public PubResult businessBillConfig(JSONObject config) throws Exception {
        return ((SuperAction) AopContext.currentProxy()).billConfig(config);
    }

    public PubResult businessBillQuery(JSONObject config) throws Exception {
        return ((SuperAction) AopContext.currentProxy()).billQuery(config);
    }

    public PubResult businessBillSave(JSONObject config) throws Exception {
        return null;
    }

    public PubResult businessBillDelete(JSONObject config) throws Exception {
        return null;
    }

    public PubResult businessBillUpdate(JSONObject config) throws Exception {
        return null;
    }

    public PubResult businessDataSourcesQuery(JSONObject config) throws Exception {
        return ((SuperAction) AopContext.currentProxy()).billDataSource(config);
    }


    /**
     * 加载配置
     * @param handler
     * @throws Exception
     */
    @Override
    public void handler(HandlerConfigChain handler) throws Exception {
        handler.addHandler(new SQ01Form_XZH());//公务外出申请单
        handler.addHandler(new AppBxsqCostDetailList());//预算费用分摊明细
        handler.addHandler(new AppBxsqZxCostDetailList());//专项费用分摊明细
        handler.addHandler(new BxCostApplyEmpList());//人员信息
        handler.addHandler(new BxsqYSCostDetailList());//新申请单预算明细
        handler.addHandler(new CostDetailList());//申请单明细
        super.handler(handler);
    }

    /**
     * 初始化配置
     *
     * @param configVO
     * @throws Exception
     */
    @Override
    public void initConfig(ConfigVO configVO) throws Exception {
        Map<String, Config> configMap = configVO.getConfigMap();
//        configMap.remove("bxCostApplyEmpList");
//        configMap.remove("costShareDetailList");
//        configMap.remove("costShareDetailListZX");
        super.initConfig(configVO);
    }

    /**
     * 获取配置
     *
     * @param configVO
     * @throws Exception
     */
    @Override
    public void config(ConfigVO configVO) throws Exception {
//        Map<String, Config> configMap = configVO.getConfigMap();
//        Config bx01Form = configMap.get("BX01Form");
//        Map<String, Object> config = bx01Form.getConfig();
//        Map<String,Object> object = (Map<String, Object>) config.get("bra_id");
//        object.put("default","css");
//        super.config(configVO);
    }

    /**
     * 初始化查询
     *
     * @param queryVO
     * @throws Exception
     */
    @Override
    public void initQuery(QueryVO queryVO) throws Exception {
        hashCache.clear();
        super.initQuery(queryVO);
    }

    /**
     * 获取查询数据
     *
     * @param queryVO
     * @throws Exception
     */
    @Override
    public void query(QueryVO queryVO) throws Exception {
        long l1 = System.currentTimeMillis();
        // 主表参数
        Map<String, Object> mainValMap = queryVO.getMainParams();
        // 主数据
        Map<String, Object> bill = queryVO.getMainData();

        // 单据类型主键
        // 当前单据用户
        String bbd_id = PubConvert.ToString(bill.get("bbd_id"));
        String user_id = PubConvert.ToString(bill.get("user_id"));

        //单据分摊类型
        String bbd_cost_type = getCostTypeByBBD(bbd_id);
        bill.put("bbd_cost_type", bbd_cost_type);
        //单据已提交是否允许修改附件

        long l2 = System.currentTimeMillis();
        String link_order_type = PubConvert.ToString(mainValMap.get("link_order_type"));
        String is_submit_editfile = getIsSubmitEditfile(link_order_type);
        bill.put("is_submit_editfile", is_submit_editfile);
        long l3 = System.currentTimeMillis();

        System.out.println("query单据已提交是否允许修改附件：" + (l3 - l3));


        //当前用户人员名称 ----
        //当前用户人员职称编码
        //当前用户人员职称编码

        long l4 = System.currentTimeMillis();
        Map<String, String> persionNameAndTeachMap = getPersionNameAndTeach(user_id);
        bill.put("ca_user", user_id);
        bill.put("ca_user_text", persionNameAndTeachMap.get("persion_name"));
        bill.put("persion_teach_title", persionNameAndTeachMap.get("persion_teach_title"));//
        bill.put("technical", persionNameAndTeachMap.get("persion_teach_title"));
        long l5= System.currentTimeMillis();
        System.out.println("query用户职称：" + (l5 - l4));


        //当前用户人员职称名称
        long l6 = System.currentTimeMillis();
        String technical = getSysDataCodeTechnical(persionNameAndTeachMap.get("persion_teach_title"));
        bill.put("technical_text", technical);
        long l7= System.currentTimeMillis();
        System.out.println("query用户人员职称名称：" + (l7 - l6));

        long l8 = System.currentTimeMillis();
        String area_id = PubConvert.ToString(bill.get("area"));
        bill.put("area_text", getAreaName(area_id));
        long l9= System.currentTimeMillis();
        System.out.println("query地区：" + (l9 - l8));

        //同行人数量
        long l10 = System.currentTimeMillis();
        Map<String, String> partnerCount = queryPartnerCount(bbd_id);
        bill.put("partnerCount", partnerCount.get("num"));
        String companyID = CurrentUser.getUserCurrenCompanyID();
        Map<String, String> paramPropertyValue = queryValueByComAndCode(companyID);
        bill.putAll(paramPropertyValue);
        long l11 = System.currentTimeMillis();
        System.out.println("query同行人：" + (l11 - l10));

        //当前人员对应的用户
        long l12 = System.currentTimeMillis();
        Map<String, String> empInfo = getEmpInfo();
        bill.put("empInfo", empInfo);
        String emp_ids = getAgentIDListStr(empInfo.get("bhe_id"), user_id);
        bill.put("emp_ids", emp_ids);
        long l13 = System.currentTimeMillis();
        System.out.println("query对应用户：" + (l13 - l12));

        long ll2 = System.currentTimeMillis();

        System.out.println("query时间：" + (ll2 - l1));
        super.query(queryVO);
    }

    @Override
    public void reference(QueryVO queryVO) throws Exception {
        long l1 = System.currentTimeMillis();
        ConfigQuery mainConfig = queryVO.getConfigMap().get("main");
        Map<String, Object> mainMap = mainConfig.getValMap();
        //表单编码
        String formCode = PubConvert.ToString(mainMap.get("formCode"));
        // 主数据
        Map<String, Object> bill = queryVO.getMainData();
        // 用户
        // 组织科室
        // 所有代理人员
        // 单据类型
        String user_id = PubConvert.ToString(bill.get("user_id"));
        String companyID = CurrentUser.getUserCurrenCompanyID();
        String emp_ids = PubConvert.ToString(bill.get("emp_ids"));
        String bbd_id = PubConvert.ToString(bill.get("bbd_id"));
        Map<String, Object> empInfo = PubConvert.beanToMap(bill.get("empInfo"));


        // 费用明细
        List<Map<String, String>> costDetail = queryCostDetailForSecect(companyID, bbd_id);
        bill.put("costDetail", costDetail);

        //代理人下拉列表
        List<Map<String, Object>> agentList = getAgentList(emp_ids);
        SourceQueue.put(new Reference().JSONSelect(formCode, "ca_user", agentList));

        new Reference().dicSelect("org_id","YESORNO");
        //科室下拉框
        Map<String, List<Map<String, Object>>> orgsAll = getOrgsAll(emp_ids, companyID);
        List<Map<String, Object>> orgs = getOrgs(orgsAll, user_id, PubConvert.ToString(empInfo.get("bhe_id")));
        SourceQueue.put(new Reference().JSONSelect(formCode, "org_id", orgs));

        long l2 = System.currentTimeMillis();

        System.out.println("reference时间：" + (l2 - l1));
        super.reference(queryVO);
    }
}
